<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="进店客户名称">
              <a-input
                placeholder="请输入进店客户名称"
                v-decorator="['customerName', { rules: [{ required: true, message: '请输入进店客户名称' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户手机号码">
              <a-input
                placeholder="请输入客户手机号码"
                v-decorator="['contactPhone', { rules: [{ required: true, message: '请输入客户手机号码' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户证件类型">
              <!-- <a-input
                placeholder="请输入客户证件类型"
                v-decorator="['certType', { rules: [{ required: true, message: '请输入客户证件类型' }] }]"
              />-->
              <a-select
                placeholder="请选择客户证件类型"
                v-decorator="['certType', { rules: [{ required: false, message: '请输入客户证件类型' }] }]"
              >
                <a-select-option value>请选择</a-select-option>
                <a-select-option value="1">身份证</a-select-option>
                <a-select-option value="2">护照</a-select-option>
                <a-select-option value="3">士官证</a-select-option>
                <a-select-option value="4">居住证</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="进店客户证件号">
              <a-input
                placeholder="请输入进店客户证件号"
                v-decorator="['certNo', { rules: [{ required: false, message: '请输入进店客户证件号' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="品牌名称">
              <!-- <a-input
                placeholder="请输入品牌名称"
                v-decorator="['brand', { rules: [{ required: true, message: '请输入品牌名称' }] }]"
              />-->
              <a-select
                @change="changeBrand"
                placeholder="请输入品牌名称"
                v-decorator="['brand', { rules: [{ required: false, message: '请输入品牌名称' }] }]"
              >
                <a-select-option value>请选择</a-select-option>
                <a-select-option
                  :value="value"
                  v-for="(value,key) in brandList"
                  :key="key"
                >{{ value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="汽车车型">
              <!-- <a-input
                placeholder="请输入汽车车型"
                v-decorator="['carModel', { rules: [{ required: true, message: '请输入汽车车型' }] }]"
              />-->
              <a-select
                placeholder="请输入汽车车型" v-decorator="['carModel', { rules: [{ required: false, message: '请输入汽车车型' }] }]"
              >
                <a-select-option value>请选择</a-select-option>
                <a-select-option
                  :value="value"
                  v-for="(value,key) in carModel"
                  :key="key"
                >{{ value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="车辆颜色要求">
              <a-select
                placeholder="请输入车辆颜色"
                v-decorator="['outsideColor', { rules: [{ required: false, message: '请输入车辆颜色要求' }] }]"
              >
                <a-select-option value>请选择</a-select-option>
                <a-select-option
                  :value="value"
                  v-for="(value,key) in colorList"
                  :key="key"
                >{{ value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内饰颜色要求">
              <a-select
                placeholder="请输入内饰颜色"
                v-decorator="['interiorColor', { rules: [{ required: false, message: '请输入内饰颜色要求' }] }]"
              >
                <a-select-option value>请选择</a-select-option>
                <a-select-option
                  :value="value"
                  v-for="(value,key) in inColorList"
                  :key="key"
                >{{ value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="汽车类型">
              <a-select
                @change="changeDepositNotes"
                v-decorator="['powerType', { rules: [{ required: false, message: '请选择汽车类型' }] }]"
                placeholder="请选择汽车类型"
              >
                <a-select-option value>请选择汽车类型</a-select-option>
                <a-select-option value="1">传统车</a-select-option>
                <a-select-option value="2">新能源</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'IntoStoreModel',
  data() {
    return {
      title: '交易信息',
      visible: false,
      model: {},
      saleSource: [],
      SaleInfosLike: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: '/sale/cxmSaleIntention/addIntention',
        edit: '/sale/cxmSaleIntention/editIntention',
        brandList: '/sale/cxmSaleBaseInfo/queryByTypes',
      },
      showDing: false,
      showEnterpriseUscc: false,
      brandList: [],
      brandListAll: [],
      colorList: [],
      carModel: [],
      carModelAll: [],
      inColorList: [],
    }
  },
  created() {
    getAction(this.url.brandList + "?dataTypes=1,2,8,9").then((res) => {
      if (res.success) {
        console.log(res.result['1']);
        this.brandListAll = res.result['1'];
        this.carModelAll = res.result['8'];
        for (let i = 0; i < res.result['1'].length; i++) {
          this.brandList.push(res.result['1'][i].name)
        }
        for (let i = 0; i < res.result['2'].length; i++) {
          this.colorList.push(res.result['2'][i].name)
        }
        for (let i = 0; i < res.result['8'].length; i++) {
          this.carModel.push(res.result['8'][i].name)
        }
        for (let i = 0; i < res.result['9'].length; i++) {
          this.inColorList.push(res.result['9'][i].name)
        }
        console.log(res.result['6']);
      }
    })
  },
  methods: {
    changeDepositNotes(val) {
      if (val == '不可退') {
        this.showDing = true
      } else {
        this.showDing = false
      }
    },
    add() {
      const that = this;
      that.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      console.log(this.model);
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue({
          customerName: this.model.customerName,
          contactPhone: this.model.contactPhone,
          certType: this.model.certType?String(this.model.certType):null,
          certNo: this.model.certNo,
          carModel: this.model.carModel,
          brand: this.model.brand,
          outsideColor: this.model.outsideColor,
          interiorColor: this.model.interiorColor,
          powerType: this.model.powerType,
        })

      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!that.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(that.model);
          let formData = Object.assign(that.model, {
              customerName: values.customerName,
              certNo: values.certNo,
              certType: Number(values.certType),
              contactPhone: values.contactPhone,
              brand: values.brand,
              carModel: values.carModel,
              interiorColor: values.interiorColor,
              outsideColor: values.outsideColor,
              powerType: values.powerType,
              id: that.model.id
          })
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    changeBrand(value) {
      this.carModel = [];
      this.form.setFieldsValue({
        "carModel": []
      })
      for (let i = 0; i < this.brandListAll.length; i++) {
        if (this.brandListAll[i].name == value) {
          for (let j = 0; j < this.carModelAll.length; j++) {
            if (this.carModelAll[j].parentId == this.brandListAll[i].id) {
              this.carModel.push(this.carModelAll[j].name);
            }
          }
        }
      }
    }
  },
}
</script>

<style lang="less" scoped>
</style>